package x.ovo.wechat.bot.plugin;

import com.kennycason.kumo.CollisionMode;
import com.kennycason.kumo.WordCloud;
import com.kennycason.kumo.WordFrequency;
import com.kennycason.kumo.bg.CircleBackground;
import com.kennycason.kumo.font.KumoFont;
import com.kennycason.kumo.font.scale.SqrtFontScalar;
import com.kennycason.kumo.image.AngleGenerator;
import com.kennycason.kumo.palette.ColorPalette;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import redis.clients.jedis.resps.Tuple;

import java.awt.*;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@UtilityClass
public class WordCloudHrlper {

    public static void main(String[] args) {
        List<Tuple> list = new ArrayList<>();
        list.add(new Tuple("老婆", 32.0));
        list.add(new Tuple("看看", 25.0));
        list.add(new Tuple("超超", 16.0));
        list.add(new Tuple("没有", 14.0));
        list.add(new Tuple("老公", 13.0));
        list.add(new Tuple("河北", 13.0));
        list.add(new Tuple("妹子", 12.0));
        list.add(new Tuple("废物", 11.0));
        list.add(new Tuple("变态", 11.0));
        list.add(new Tuple("贡献", 8.0));
        list.add(new Tuple("知道", 8.0));
        list.add(new Tuple("浪费", 8.0));
        list.add(new Tuple("时候", 8.0));
        list.add(new Tuple("刘亦菲", 8.0));
        list.add(new Tuple("资源", 7.0));
        list.add(new Tuple("社会", 7.0));
        list.add(new Tuple("相亲", 7.0));
        list.add(new Tuple("活着", 7.0));
        list.add(new Tuple("正常", 7.0));
        list.add(new Tuple("小晗", 7.0));
        list.add(new Tuple("可 以", 7.0));
        list.add(new Tuple("鱼狗", 6.0));
        list.add(new Tuple("超出", 6.0));
        list.add(new Tuple("群友", 6.0));
        list.add(new Tuple("照片", 6.0));
        list.add(new Tuple("李婷", 6.0));
        list.add(new Tuple("意思", 6.0));
        list.add(new Tuple("应该", 6.0));
        list.add(new Tuple("喜欢", 6.0));
        list.add(new Tuple("emoji", 6.0));
        list.add(new Tuple("飞机", 5.0));
        list.add(new Tuple("见过", 5.0));
        list.add(new Tuple("考虑", 5.0));
        list.add(new Tuple("结婚照", 5.0));
        list.add(new Tuple("管理", 5.0));
        list.add(new Tuple("直接", 5.0));
        list.add(new Tuple("手柄", 5.0));
        list.add(new Tuple("利息", 5.0));
        list.add(new Tuple("进来", 4.0));
        list.add(new Tuple("还要", 4.0));
        list.add(new Tuple("过来", 4.0));
        list.add(new Tuple("贫穷", 4.0));
        list.add(new Tuple("美女", 4.0));
        list.add(new Tuple("特产", 4.0));
        list.add(new Tuple("消息", 4.0));
        list.add(new Tuple("新人", 4.0));
        list.add(new Tuple("情感", 4.0));
        list.add(new Tuple("工资", 4.0));
        list.add(new Tuple("山西", 4.0));
        list.add(new Tuple("妄想症", 4.0));
        list.add(new Tuple("头像", 4.0));
        list.add(new Tuple("出现", 4.0));
        list.add(new Tuple("@LPC", 4.0));
        list.add(new Tuple("雷总", 3.0));
        list.add(new Tuple("零花钱", 3.0));
        list.add(new Tuple("阿雷", 3.0));
        list.add(new Tuple("阿訇", 3.0));
        list.add(new Tuple("问题", 3.0));
        list.add(new Tuple("键盘", 3.0));
        list.add(new Tuple("邯郸", 3.0));
        list.add(new Tuple("运城", 3.0));
        list.add(new Tuple("贪污", 3.0));
        list.add(new Tuple("记得", 3.0));
        list.add(new Tuple("猪油", 3.0));
        list.add(new Tuple("没事", 3.0));
        list.add(new Tuple("机器人", 3.0));
        list.add(new Tuple("摸鱼", 3.0));
        list.add(new Tuple("找对象", 3.0));
        list.add(new Tuple("才是", 3.0));
        list.add(new Tuple("忘了", 3.0));
        list.add(new Tuple("形容", 3.0));
        list.add(new Tuple("开光", 3.0));
        list.add(new Tuple("广智", 3.0));
        list.add(new Tuple("小笼包", 3.0));
        list.add(new Tuple("小号", 3.0));
        list.add(new Tuple("宝马", 3.0));
        list.add(new Tuple("嫂子", 3.0));
        list.add(new Tuple("好像", 3.0));
        list.add(new Tuple("大哥", 3.0));
        list.add(new Tuple("发言", 3.0));
        list.add(new Tuple("出去", 3.0));
        list.add(new Tuple("凉快", 3.0));
        list.add(new Tuple("关注", 3.0));
        list.add(new Tuple("人民", 3.0));
        list.add(new Tuple("专家", 3.0));
        list.add(new Tuple("不能", 3.0));
        list.add(new Tuple("下雨天", 3.0));
        list.add(new Tuple("上班", 3.0));
        list.add(new Tuple("上去", 3.0));
        list.add(new Tuple("tm", 3.0));
        list.add(new Tuple("span>", 3.0));
        list.add(new Tuple("class", 3.0));
        list.add(new Tuple("@alei", 3.0));
        list.add(new Tuple("><", 1.0));
        list.add(new Tuple("<span", 3.0));
        list.add(new Tuple("鱼果", 2.0));
        list.add(new Tuple("饿着", 2.0));
        list.add(new Tuple("随便", 2.0));
        list.add(new Tuple("陪嫁", 2.0));
        list.add(new Tuple("陈醋", 2.0));
        File file = new File("C:\\Users\\Administrator\\Pictures", "wordcloud.png");
        generate(list, file);
    }


    public static void generate(List<Tuple> data, File file) {
        log.debug("词云数据：{}", data);
        List<WordFrequency> list = data.stream()
                .map(e -> new WordFrequency(e.getElement(), Double.valueOf(e.getScore()).intValue()))
//                .collect(Collectors.groupingBy(WordFrequency::getWord",  Collectors.summingInt(WordFrequency::getFrequency)))
//                .entrySet().stream()
//                .map(e -> new WordFrequency(e.getKey()",  e.getValue()))
                .collect(Collectors.toList());

        WordCloud wordCloud = getWordCloud();
        wordCloud.build(list);
        //生成词云图路径
        wordCloud.writeToFile(file.getPath());
    }

    private static WordCloud getWordCloud() {
        //设置图片分辨率
        Dimension dimension = new Dimension(1000, 1000);
        //此处的设置采用内置常量即可，生成词云对象
        WordCloud wordCloud = new WordCloud(dimension, CollisionMode.PIXEL_PERFECT);
        //设置边界及字体
        wordCloud.setPadding(5);
        //设置圆的半径
        wordCloud.setBackground(new CircleBackground(500));
        //字号的大小范围，最小是多少，最大是多少
        wordCloud.setFontScalar(new SqrtFontScalar(15, 120));
        // 设置偏转角，角度为0时，字体都是水平的
        wordCloud.setAngleGenerator(new AngleGenerator(0));
        //设置词云显示的颜色，越靠前设置表示词频越高的词语的颜色
        ColorPalette palette = new ColorPalette(0xee5253, 0xff9f43, 0x2e86de, 0x10ac84, 0x0abde3, 0xf368e0, 0x5f27cd, 0x8395a7);
        wordCloud.setColorPalette(palette);
        //此处不设置会出现中文乱码
        Font font = new Font("simfang", Font.PLAIN, 18);
        wordCloud.setKumoFont(new KumoFont(font));
        // 设置背景颜色
        wordCloud.setBackgroundColor(Color.white);
        return wordCloud;
    }

}
